﻿// <copyright file="ResponseItem.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2008-09-23</date>
// <summary>OpenSocial Response Item</summary>

// Ignore SA1300
namespace opensocial
{
    using System.Windows.Browser;

    /// <summary>
    /// OpenSocial Response Item
    /// </summary>
    public class ResponseItem
    {
        /// <summary>
        /// response item Script object
        /// </summary>
        private ScriptObject responseItem;

        /// <summary>
        /// Initializes a new instance of the ResponseItem class.
        /// </summary>
        /// <param name="responseItem">response item script object</param>
        internal ResponseItem(ScriptObject responseItem)
        {
            this.responseItem = responseItem;
        }

        /// <summary>
        /// did the response have an error?
        /// </summary>
        /// <returns>if the response had an error</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "had", Justification = "Mirroring OpenSocial API.")]
        public bool hadError()
        {
            return (bool) this.responseItem.Invoke("hadError");
        }

        /// <summary>
        /// Get the error code
        /// </summary>
        /// <returns>the error code</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Mirroring OpenSocial API."), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "get", Justification = "Mirroring OpenSocial API.")]
        public string getErrorCode()
        {
            return this.responseItem.Invoke("getErrorCode") as string;
        }

        /// <summary>
        /// Get the error message
        /// </summary>
        /// <returns>the error message</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Mirroring OpenSocial API."), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "get", Justification = "Mirroring OpenSocial API.")]
        public string getErrorMessage()
        {
            var errorMessage = this.responseItem.Invoke("getErrorMessage");

            var errorMessageString = errorMessage as string;

            if (errorMessageString != null)
            {
                return errorMessageString;
            }

            ScriptObject error = errorMessage as ScriptObject;

            if (error == null)
            {
                return string.Empty;
            }

            return error.GetProperty("message") as string;
        }

        /// <summary>
        /// Get the a data collection
        /// </summary>
        /// <returns>the data collection</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Mirroring OpenSocial API."), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "get", Justification = "Mirroring OpenSocial API.")]
        public ScriptObject getData()
        {
            var collection = this.responseItem.Invoke("getData", null) as ScriptObject;

            return collection;
        }
    }
}
